/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.service.impl;

import java.util.Collection;

import org.richfaces.application.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.UserDao;
import ar.com.greensoft.neo.model.Authority;
import ar.com.greensoft.neo.model.User;
import ar.com.greensoft.neo.service.UserService;

/**
*
* @author Jorge E. Villaverde
* @since 1.0
*/
@Service("userService")
public class UserServiceImpl implements UserService {
	private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private PasswordEncoder passwordEncoder;

	public User loadUser(String username) throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Loading User:" + username);
		try {
			return userDao.findById(username);
		} catch (DaoException e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	public void saveUser(User user) throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Saving User:" + user);
		try {
			userDao.merge(user);
		} catch (DaoException e) {
			logger.error(e.getMessage());
		}
	}

	@Override
	public User findUserByUsername(String username) throws ServiceException  {
		if(logger.isTraceEnabled())
			logger.trace("Searching Users with username:" + username);
		try {
			return userDao.findUserByUsername(username);
		} catch (DaoException e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public Collection<User> getUsers() throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Retrieving all Users");
		Collection<User> ret = null;
		try {
			ret = userDao.findAll();
			if(logger.isTraceEnabled())
				logger.trace((ret == null)? "No Users Found." : ret.size() + " Users Found");
		} catch (DaoException e) {
			logger.error(e.getMessage());
		}
		return ret;
	}

	@Override
	public User createUser(String username, String password) throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Creating User: " + username);
		try {
			User user = new User();
			user.setUsername(username);
			user.setPassword(passwordEncoder.encodePassword(password, null));
			user.setEnabled(true);

			userDao.persist(user);
			
			return user;
		} catch (DaoException e) {
			logger.error(e.getMessage());
			throw new ServiceException(e.getMessage());
		}		
	}

	@Override
	public void grantAuthority(Authority authority) throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Adding Authority: " + authority);
		try {
			userDao.addAuthority(authority);
		} catch (DaoException e) {
			logger.error(e.getMessage());
			throw new ServiceException(e.getMessage());
		}		
		
	}
	
	@Override
	public void revokeAuthority(Authority authority) throws ServiceException {
		if(logger.isTraceEnabled())
			logger.trace("Adding Authority: " + authority);
		try {
			userDao.removeAuthority(authority);
		} catch (DaoException e) {
			logger.error(e.getMessage());
			throw new ServiceException(e.getMessage());
		}		
		
	}	
}